using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GamePlay;
using MainGame.GameSound;

namespace MainGame.Menu
{

  public interface IHelpMenu
  {
    event MenuEvent OnHBackClicked;
  }

  public class HelpMenu : BaseMenu, IHelpMenu
  {

    Button helpBackBtn;
    Vector2 savedPos;
    public bool Animated = false;
    public int Direction = 1, AnimVelocity = 60;    
    SpriteBatch spriteBatch;

    public HelpMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      spriteBatch = Game.GetContext().SpriteBatch;
      background = game.Content.Load<Texture2D>(@"Images//Menu//HelpBg");
      bounds = new Rectangle(-game.GraphicsDevice.Viewport.Width, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
      game.Services.AddService(typeof(IHelpMenu), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(IHelpMenu));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      savedPos.X = bounds.X;
      
      
      // Load buttons
      helpBackBtn = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 620, 350, 140, 55), Color.Blue, "small_font", Locale.Strings.Back, Color.White);
      AddButton(helpBackBtn, () =>
      {
        
        var ui_renderer = Game.Services.GetService(typeof(IUIRenderer)) as UIRenderer;
        if (ui_renderer != null)
        {
          foreach (ClickableButton cb in ui_renderer.buttons)
            if (cb.b.color == Color.Red)
              cb.b.color = Color.White;          
        } 
        
        Animated = true;
        Direction = -1;
      });

      base.Initialize();
    }


    public override void Update(GameTime gameTime)
    {
      // Input: back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        //SoundManager.PlaySound(MenuSounds.ClickCard);
        
        var ui_renderer = Game.Services.GetService(typeof(IUIRenderer)) as UIRenderer;
        if (ui_renderer != null)
        {
          foreach (ClickableButton cb in ui_renderer.buttons)
            if (cb.b.color == Color.Red)
              cb.b.color = Color.White;
        }

        Animated = true;
        Direction = -1;
      }

      if (Animated)
      {
        // Animate the menu items to follow the menu sliding animation
        bounds.X += Direction * AnimVelocity;

        foreach (Button b in (from cb in buttons select cb.b))
          b.bounds.X += Direction * AnimVelocity;

        if (bounds.X > ((Game.GraphicsDevice.Viewport.Width - bounds.Width) / 2) && Direction == 1)
        {
          Animated = false;
          int offset = bounds.X ;
          bounds.X = ((Game.GraphicsDevice.Viewport.Width - bounds.Width) / 2);

          foreach (Button b in (from cb in buttons select cb.b))
            b.bounds.X += -Direction * offset;
        }

        if (bounds.X < savedPos.X)
        {
          if (OnHBackClicked != null)
            OnHBackClicked();
        }
      }

      base.Update(gameTime);
    }


    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);

      spriteBatch.Begin();

      spriteBatch.DrawString(Game.Content.Load<SpriteFont>("small_font"), Locale.Strings.HelpScr01, new Vector2(bounds.X + 40, bounds.Y + 125), Color.Green, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0f);
      spriteBatch.DrawString(Game.Content.Load<SpriteFont>("small_font"), Locale.Strings.HelpScr02.Replace("\\n", "\n"), new Vector2(bounds.X + 280, bounds.Y + 180), Color.Green, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0f);
      spriteBatch.DrawString(Game.Content.Load<SpriteFont>("small_font"), Locale.Strings.HelpScr03, new Vector2(bounds.X + 40, bounds.Y + 295), Color.Green, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0f);
      spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Menu//2SixHelp"), new Rectangle((int)bounds.X + 10, bounds.Y +  165, 250, 119), Color.White);
      spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Menu//CrossroadsArrow"), new Rectangle((int)bounds.X + 73, bounds.Y + 195, 126, 46), Color.White);

      spriteBatch.End();
    }

    public event MenuEvent OnHBackClicked;
  }
}
